package com.yami.shop.rights.common.listener;

import com.alibaba.fastjson.JSONObject;
import com.yami.shop.bean.enums.RightsType;
import com.yami.shop.bean.event.ExchangeVerificationRightsEvent;
import com.yami.shop.bean.model.UserAddr;
import com.yami.shop.bean.model.UserAddrOrder;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.rights.common.model.ExchangeRights;
import com.yami.shop.rights.common.model.PersonRights;
import com.yami.shop.rights.common.service.ExchangeRightsService;
import com.yami.shop.rights.common.service.PersonRightsService;
import com.yami.shop.rights.common.strategy.PersonRightsFactory;
import com.yami.shop.service.UserAddrOrderService;
import com.yami.shop.service.UserAddrService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component("ExchangeVerificationRightsListener")
@AllArgsConstructor
public class ExchangeVerificationRightsListener {

    @Autowired
    private PersonRightsFactory personRightsFactory;
    @Autowired
    private UserAddrOrderService userAddrOrderService;
    @Autowired
    private UserAddrService userAddrService;
    @Autowired
    private PersonRightsService personRightsService;
    @Autowired
    private ExchangeRightsService exchangeRightsService;

    @EventListener(ExchangeVerificationRightsEvent.class)
    public void updateKtPersonRights(ExchangeVerificationRightsEvent event) {
        log.info("==========兑换权益核销======{}",event.getPersonRightsId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", RightsType.Exchange_VOUCHER.value());
        jsonObject.put("personRightsId", event.getPersonRightsId());
        // 地址ID 为用户地址，需存入用户订单地址表中 其主键轮转到 权益兑换记录表中
        long addressId = event.getAddressId();
        UserAddr byId = userAddrService.getById(addressId);
        // 把订单地址保存到数据库
        UserAddrOrder userAddrOrder = com.yami.shop.common.util.BeanUtil.map(byId, UserAddrOrder.class);
        if (userAddrOrder == null) {
            // 请填写收货地址
            throw new YamiShopBindException("yami.delivery.address");
        }
        userAddrOrder.setCreateTime(new Date());
        userAddrOrder.setUserId(byId.getUserId());
        userAddrOrderService.save(userAddrOrder);
//        jsonObject.put("exchangeRightsId", event.getPersonRightsId());
//        ExchangeRights exchangeRights = exchangeRightsService.getById(event.getPersonRightsId());
        PersonRights personRights = personRightsService.getById(event.getPersonRightsId());
        jsonObject.put("addressId", userAddrOrder.getAddrOrderId());
        jsonObject.put("personRightsId", personRights.getId());
        jsonObject.put("prodId", event.getProdId());
        jsonObject.put("rightsId", personRights.getRightsId());
        jsonObject.put("tokenId", personRights.getTokenId());
        jsonObject.put("userId", byId.getUserId());
        personRightsFactory.verificationRights(jsonObject);
    }

}
